package com.example.community.service;

import com.example.community.dao.LoginTicketMapper;
import com.example.community.dao.UserMapper;
import com.example.community.entity.LoginTicket;
import com.example.community.entity.MailEvent;
import com.example.community.entity.User;
import com.example.community.event.EventProducer;
import com.example.community.util.CommunityUtil;
import com.example.community.util.MailClient;
import com.example.community.util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.example.community.util.CommunityConstant.*;

@Service
public class UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailClient mailClient;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EventProducer eventProducer;

    //@Autowired
    //private LoginTicketMapper loginTicketMapper;

    @Value("${community.path.domain}")
    private String domain;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    public User findUserById(int id) {
        //return userMapper.selectById(id);
        //从redis中取得本次用户
        User user = getCache(id);
        if(user == null){
            user = initCache(id);
        }
        return user;
    }

    public int updatePassword(int id, String password){
        int row = userMapper.updatePassword(id,password);
        clearCache(id);
        return row;
    }

    public Map<String, Object> register(User user) {
        Map<String, Object> map = new HashMap<>();

        // 空值处理
        if (user == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }
        if (StringUtils.isBlank(user.getUsername())) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if (StringUtils.isBlank(user.getPassword())) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }
        if (StringUtils.isBlank(user.getEmail())) {
            map.put("emailMsg", "邮箱不能为空!");
            return map;
        }

        // 验证账号
        User u = userMapper.selectByName(user.getUsername());
        if (u != null) {
            map.put("usernameMsg", "该账号已存在!");
            return map;
        }

        // 验证邮箱
        u = userMapper.selectByEmail(user.getEmail());
        if (u != null) {
            map.put("emailMsg", "该邮箱已被注册!");
            return map;
        }

        // 注册用户
        user.setSalt(CommunityUtil.generateUUID().substring(0, 5));
        user.setPassword(CommunityUtil.md5(user.getPassword() + user.getSalt()));
        user.setType(0);
        user.setStatus(0);
        user.setActivationCode(CommunityUtil.generateUUID());
        user.setHeaderUrl(String.format("http://images.nowcoder.com/head/%dt.png", new Random().nextInt(1000)));
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        //触发发送激活邮件事件
        MailEvent mailEvent = new MailEvent();
        mailEvent.setTopic(TOPIC_MAIL);
        mailEvent.setEmail(user.getEmail());

        // http://localhost:8080/community/activation/101/code
        mailEvent.setUrl(domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode());

        eventProducer.sendMail(mailEvent);

        return map;
//         //激活邮件
//        Context context = new Context();
//        context.setVariable("email", user.getEmail());
//        // http://localhost:8080/community/activation/101/code
//        String url = domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode();
//        context.setVariable("url", url);
//        String content = templateEngine.process("/mail/activation", context);
//        mailClient.sendMail(user.getEmail(), "激活账号", content);
//        sendMail(user);

    }
//    @Async
//    public void sendMail(User user){
//        // 激活邮件
//        Context context = new Context();
//        context.setVariable("email", user.getEmail());
//        // http://localhost:8080/community/activation/101/code
//        String url = domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode();
//        context.setVariable("url", url);
//        String content = templateEngine.process("/mail/activation", context);
//        mailClient.sendMail(user.getEmail(), "激活账号", content);
//    }

    public int activation(int userId, String code) {
        User user = userMapper.selectById(userId);
        if (user.getStatus() == 1) {
            return ACTIVATION_REPEAT;
        } else if (user.getActivationCode().equals(code)) {
            userMapper.updateStatus(userId, 1);
            //清理redis中的缓存
            clearCache(userId);

            return ACTIVATION_SUCCESS;
        } else {
            return ACTIVATION_FAILURE;
        }
    }

    public Map<String, Object> login(String username, String password, int expiredSeconds) {
        Map<String, Object> map = new HashMap<>();

        // 空值处理
        if (StringUtils.isBlank(username)) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if (StringUtils.isBlank(password)) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }

        // 验证账号
        User user = userMapper.selectByName(username);
        if (user == null) {
            map.put("usernameMsg", "该账号不存在!");
            return map;
        }

        // 验证状态
        if (user.getStatus() == 0) {
            map.put("usernameMsg", "该账号未激活!");
            return map;
        }

        // 验证拉黑状态
        if (user.getStatus() == 3) {
            map.put("usernameMsg", "该账号被禁止登录!");
            return map;
        }

        // 验证密码
        password = CommunityUtil.md5(password + user.getSalt());
        if (!user.getPassword().equals(password)) {
            map.put("passwordMsg", "密码不正确!");
            return map;
        }

        // 生成登录凭证
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(user.getId());
        loginTicket.setTicket(CommunityUtil.generateUUID());
        loginTicket.setStatus(0);
        loginTicket.setExpired(new Date(System.currentTimeMillis() + (expiredSeconds * 1000)));
       // loginTicketMapper.insertLoginTicket(loginTicket);

        String redisKey = RedisKeyUtil.getTicketKey(loginTicket.getTicket());
        redisTemplate.opsForValue().set(redisKey,loginTicket);

        map.put("ticket", loginTicket.getTicket());
        return map;
    }
    public void logout(String ticket) {
        String redisKey = RedisKeyUtil.getTicketKey(ticket);
        LoginTicket loginTicket = (LoginTicket) redisTemplate.opsForValue().get(redisKey);
        loginTicket.setStatus(1);
        redisTemplate.opsForValue().set(redisKey,loginTicket);
//        System.out.println(loginTicketMapper.selectByTicket(ticket).getExpired().toString());
//        loginTicketMapper.updateStatus(ticket, 1);
//        System.out.println(loginTicketMapper.selectByTicket(ticket).getExpired().toString());
    }
    public LoginTicket findLoginTicket(String ticket) {
        String redisKey = RedisKeyUtil.getTicketKey(ticket);
        return (LoginTicket) redisTemplate.opsForValue().get(redisKey);
        //return loginTicketMapper.selectByTicket(ticket);
    }

    public int updateHeader(int userId,String headUrl){
        //清理redis中的缓存
        //clearCache(userId);
        int rows = userMapper.updateHeader(userId,headUrl);
        clearCache(userId);
        return rows;
    }
    public Map<String, String> updatePassword(String inputPassword, String newPassword, String confirmPassword){
        Map<String, String> mapPwd = new HashMap<>();
        if (inputPassword == null){
            mapPwd.put("inputMsg","请输入原密码");
            return mapPwd;
        }
        if (newPassword == null){
            mapPwd.put("inputNewMsg","请输入新密码");
            return mapPwd;
        }
        if (confirmPassword == null){
            mapPwd.put("inputConfirmMsg","请输入确认密码");
            return mapPwd;
        }
        if (!newPassword.equals(confirmPassword)){
            mapPwd.put("inputConfirmMsg","新密码与确认密码不一致");
            return mapPwd;
        }
        return mapPwd;
    }
    public User findUserByName(String username){
        return userMapper.selectByName(username);
    }
    // 1.优先从缓存中取值
    private User getCache(int userId) {
        String redisKey = RedisKeyUtil.getUserKey(userId);
        System.out.println("--------------------");
        System.out.println("--在redis找到该用户---");
        System.out.println("--------------------");
        return (User) redisTemplate.opsForValue().get(redisKey);
    }

    // 2.取不到时初始化缓存数据
    private User initCache(int userId) {
        User user = userMapper.selectById(userId);
        System.out.println("--------------------");
        System.out.println("--将用户保存到redis---");
        System.out.println("--------------------");
        String redisKey = RedisKeyUtil.getUserKey(userId);
        redisTemplate.opsForValue().set(redisKey, user, 3600, TimeUnit.SECONDS);
        return user;
    }

    // 3.数据变更时清除缓存数据
    //外面修改了密码
    private void clearCache(int userId) {
        System.out.println("--------------------");
        System.out.println("----redis缓存失效----");
        System.out.println("--------------------");
        String redisKey = RedisKeyUtil.getUserKey(userId);
        redisTemplate.delete(redisKey);
    }

    public Collection<? extends GrantedAuthority> getAuthorities(int userId) {
        User user = this.findUserById(userId);

        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new GrantedAuthority() {

            @Override
            public String getAuthority() {
                switch (user.getType()) {
                    case 1:
                        return AUTHORITY_ADMIN;
                    case 2:
                        return AUTHORITY_MODERATOR;
                    default:
                        return AUTHORITY_USER;
                }
            }
        });
        return list;
    }

    public User findUserByEmail(String email) {
        return userMapper.selectByEmail(email);
    }

    public void sendCode(String email, HttpServletResponse response) {
        String text = CommunityUtil.generateUUID().substring(0,6);
        // 验证码的归属
        String codeOwner = CommunityUtil.generateUUID();
        Cookie cookie = new Cookie("codeOwner", codeOwner);
        //失效的时间是10min
        cookie.setMaxAge(60*10);
        cookie.setPath(contextPath);
        response.addCookie(cookie);
        // 将验证码存入Redis,失效的时间是10min
        String redisKey = RedisKeyUtil.getCodeKey(codeOwner);
        redisTemplate.opsForValue().set(redisKey, text, 60*10, TimeUnit.SECONDS);

        // 激活邮件,使用thymeleaf创建的对象携带变量
        Context context = new Context();
        context.setVariable("email", email);
        context.setVariable("text", text);
        //使用模板引擎，利用thymeleaf，将context放到/mail/activation.html文件中，然后再利用mail包发送给邮箱
        String content = templateEngine.process("/mail/forget", context);
        mailClient.sendMail(email, "忘记密码", content);

    }

    public Map<String, Object> changePasswordByCode(String email, String password) {
        Map<String, Object> map = new HashMap<>();
        // 验证密码
        if (StringUtils.isBlank(password)) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }
        User user=userMapper.selectByEmail(email);
        password=CommunityUtil.md5(password + user.getSalt());
        userMapper.updatePassword(user.getId(),password);
        map.put("success","success");
        clearCache(user.getId());
        return map;
    }

    public int totalUser(){
        return userMapper.selectAllUser();
    }

    public List<User> findAllUser(int offset,int limit){
        return userMapper.findAllUser(offset,limit);
    }

    public List<User> findSomeUser(int offset,int limit,String keyword){
        return userMapper.findSomeUser(offset,limit,keyword);
    }

    public int updateStatus(int id,int status){
        return userMapper.updateStatus(id,status);
    }

    public List<User> findAllBlackUser(int offset,int limit){
        return userMapper.findAllBlackUser(offset,limit);
    }

    public List<User> findAdminOrModerator(int offset,int limit,int type){
        return userMapper.findAdminOrModerator(offset,limit,type);
    }

    public int totalSomeUser(String keyword){
        return userMapper.totalSomeUser(keyword);
    }
}
